package cn.edu.gduf.luowowo.mgrsite.web.controller;

import cn.edu.gduf.luowowo.article.domain.StrategyDetail;
import cn.edu.gduf.luowowo.article.query.StrategyDetailQuery;
import cn.edu.gduf.luowowo.article.service.*;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    /**
     * id 指攻略明细(文章)id
     */
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //大攻略
        model.addAttribute("strategies", strategyService.list());
        //主题
        model.addAttribute("themes", strategyThemeService.list());
        if (id != null) {
            //攻略明细
            StrategyDetail detail = strategyDetailService.get(id);
            //攻略明细内容文本
            detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
            model.addAttribute("strategyDetail", detail);
            //标签（根据当前这篇攻略明细的id去查）
            model.addAttribute("tags",strategyTagService.queryByDetailId(detail.getId()));
        }
        return "strategyDetail/input";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogService.queryByStrategyId(strategyId);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail detail, String tags) {
        strategyDetailService.saveOrUpdate(detail, tags);
        return AjaxResult.SUCCESS;
    }
}
